package com.example.icedcap.topeka_fake.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.support.annotation.ColorRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.icedcap.topeka_fake.R;
import com.example.icedcap.topeka_fake.model.Category;
import com.example.icedcap.topeka_fake.model.Theme;
import com.example.icedcap.topeka_fake.persistance.TopekaDatabaseHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: doushuqi
 * Date: 15/7/13
 * Time: 下午3:02
 * Email: shuqi.dou@singuloid.com
 */
public class CategoryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private final Resources mResources;
    public static final String DRAWABLE = "drawable";
    private static final String ICE_CATEGORY = "icon_category_";
    private final String mPackage;
    private List<Category> mCategories;


    public CategoryAdapter(Activity activity) {
        mResources = activity.getResources();
        mPackage = activity.getPackageName();
        mInflater = LayoutInflater.from(activity);
        updateCategories(activity);
    }

    @Override
    public int getCount() {
        return mCategories.size();
    }

    @Override
    public Category getItem(int position) {
        return mCategories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.item_category, parent, false);
            convertView.setTag(new CategoryViewHolder((LinearLayout)convertView));
        }
        CategoryViewHolder holder = (CategoryViewHolder) convertView.getTag();
        Category category = getItem(position);
        Theme theme = category.getTheme();
        setCategory(category, holder.icon);
        convertView.setBackgroundColor(getColor(theme.getWindowBackgroundId()));
        holder.title.setText(category.getName());
        holder.title.setTextColor(getColor(theme.getTextColorPrimaryId()));
        holder.title.setBackgroundColor(getColor(theme.getColorPrimaryId()));
        return convertView;
    }

    private void updateCategories(Activity activity) {
        //fake data
//        mCategories = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            Category category = new Category("category" + i, "food", Theme.values()[i], false);
//            mCategories.add(category);
//        }
        mCategories = TopekaDatabaseHelper.getCategories(activity, true);
    }

    private void setCategory(Category category, ImageView icon) {
        final int categoryImageResource = mResources.getIdentifier(ICE_CATEGORY
                + category.getId(), DRAWABLE, mPackage);
        final boolean solved = category.isSolved();
        if (solved) {

        } else {
            icon.setImageResource(categoryImageResource);
        }

    }

    private int getColor(@ColorRes int colorId) {
        return mResources.getColor(colorId);
    }
}
